var userTypeStore = Ext.create('Ext.data.Store', {
    fields: ['id', 'name'],
    data : [
        {"id":"1", "name":"Yönetici"},
        {"id":"2", "name":"Çalışan"},
        {"id":"3", "name":"Müşteri"}
    ]
});

Ext.define('UserForm', {
	extend : 'Ext.form.Panel',
	alias : 'widget.userform',
	closable: true,
	items : [{
		xtype : 'textfield',
		name : 'name',
		allowBlank : false,
		fieldLabel : 'Adı'
	}, {
		xtype : 'textfield',
		name : 'surname',
		allowBlank : false,
		fieldLabel : 'Soyadı'
	}, {
		xtype : 'textfield',
		name : 'username',
		allowBlank : false,
		fieldLabel : 'Kullanıcı Adı'
	}, {
		xtype : 'textfield',
		name : 'password',
		allowBlank : false,
		fieldLabel : 'Parola'
	}, {
		xtype : 'combobox',
		name : 'type',
		allowBlank : false,
		fieldLabel : 'Türü',
		store: userTypeStore,
	    queryMode: 'local',
	    displayField: 'name',
	    valueField: 'id'
	}, {
		xtype : 'hidden',
		name : 'id'
	}],
	buttons : [ {
		text : 'Kaydet',
		handler : function() {
			var form = this.up('form').getForm();
			if (form.isValid()) {
				form.submit({
					url : 'AdministrationServlet?cmd=saveUser',
					success : function(form, action) {
						Ext.Msg.alert('Success', action.result.msg);
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed', action.result.msg);
					}
				});
			}
		}
	} ],
	initComponent : function() {
		this.callParent();
		if (!Ext.isEmpty(this.userId)) {
			this.on('afterrender', function() {
				this.load({
					url: 'AdministrationServlet?cmd=getUser&id=' + this.userId
				});
			});
		}
	}
});


Ext.define('UserGrid', {
	extend : 'Ext.grid.Panel',
	closable: true,
	alias : 'widget.usergrid',
	columns : [ {
		header : 'Id',
		dataIndex : 'id'
	}, {
		header : 'Adı',
		dataIndex : 'name',
		flex : 1
	}, {
		header : 'Soyadı',
		dataIndex : 'surname',
		flex : 1
	} , {
		header : 'Kullanıcı Adı',
		dataIndex : 'username',
		flex : 1
	} , {
		header : 'Türü',
		dataIndex : 'type',
		flex : 1
	}  ],
	tbar: [{
		xtype: 'button',
		text: 'Yeni',
		handler: function (btn){
			var tabpanel = btn.up('viewport').down('tabpanel');
    		
    		tabpanel.add({
    			title: 'Kullanıcı Formu',
    			xtype: 'userform'
    		});
		}
	}, {
		xtype: 'button',
		text: 'Güncelle',
		handler: function (btn){
			var grid = btn.up('grid');
			if (grid.getSelectionModel().getSelection() <= 0) {
				alert('Satır seçiniz');
				return;
			}
    		var selectedUser = grid.getSelectionModel().getSelection()[0];
			var tabpanel = btn.up('viewport').down('tabpanel');

    		tabpanel.add({
    			title: 'Kullanıcı Formu',
    			xtype: 'userform',
    			userId: selectedUser.get('id')
    		});
		}
	}, {
		xtype: 'button',
		text: 'Tazele',
		handler: function (btn){
			var store = btn.up('grid').getStore();
			store.load();
		}
	}, '-', {
		xtype: 'textfield',
		fieldLabel: 'Adı',
		itemId: 'name'
	},  {
		xtype: 'textfield',
		fieldLabel: 'Soyadı',
		itemId: 'surname'
	}, {
		xtype: 'button',
		text: 'Ara',
		handler: function (btn) {
			var grid = btn.up('grid');
			var store = grid.getStore ();
			var name = grid.down('#name').getValue();
			var surname = grid.down('#surname').getValue();
			store.load({
				params: {
					name: name,
					surname: surname
				}
			});
		}
	}],
	store : {
		type : 'user'
	},
	initComponent : function() {

		this.callParent();
	}
});

Ext.define('UserStore', {
	extend : 'Ext.data.Store',
	alias : 'store.user',
	fields : [ 'id', 'name', 'surname', 'username', 'type' ],
	autoLoad : true,
	proxy : {
		type : 'ajax',
		url : 'AdministrationServlet?cmd=searchUser',
		reader : {
			type : 'json',
			root : 'items'
		}
	}
});